import { Message, Notice } from 'view-design';
import dayjs from "dayjs";
export default {
    namespaced: true,
    state: {
        success: [],
        successNum:0,
        successTimer:(commend)=>{
            setTimeout(commend,1000)
        },
        timer:null,
        error:[],
        errorNum:0,
        successShow:null,
        vm:null
    },
    getters: {

    },
    mutations: {
        init(state, vm) {
            state.vm=vm;
        },

    },
    actions:{
        setSuccess({state},info){
            if(!!state.timer){
                clearTimeout(state.timer)
            };
            if(state.successNum==99){
                state.success.pop();
                state.successNum=0;
            }
            let successShow={
                name:`server request -${state.successNum}`,
                title:{
                    'en-US':`server request -${state.successNum}`,
                    'zh-CN':`服务器请求 -${state.successNum}`,
                }[window.locale],
                meta:{
                    message:info.message,
                    title:{
                        'en-US':`server request -${state.successNum}`,
                        'zh-CN':`服务器请求 -${state.successNum}`,
                    }
                },
                desc:info.message[window.locale],
                duration:1,
                time: dayjs().format('YYYY-MM-DD HH:mm:ss'),
            }
            state.success.unshift(successShow);
            if(!!state.successShow) {
                Notice.close(state.successShow.name);
            }
            state.successShow=successShow;
            state.timer=setTimeout(()=>{
                Notice.success(state.successShow);
                this.state.timer=null;
            },1000)
            // Notice.success(state.successShow);
            state.successNum++;
        },
        setError({state},info){
            if(state.errorNum==99){
                state.error.pop();
                state.errorNum=0;
            }
            let errorShow={
                name:`server request -${state.errorNum}`,
                title:{
                    'en-US':`server request -${state.errorNum}`,
                    'zh-CN':`服务器请求 -${state.errorNum}`,
                }[window.locale],
                meta:{
                    message:info.message,
                    title:{
                        'en-US':`server request -${state.errorNum}`,
                        'zh-CN':`服务器请求 -${state.errorNum}`,
                    }
                },
                desc:info.message[window.locale],
                // duration:0,
                duration:15,
                time: dayjs().format('YYYY-MM-DD HH:mm:ss'),
            }
            state.error.unshift(errorShow);
            Notice.error(errorShow);
            state.errorNum++;
        }
    }
};